#include "ftc/ftc.h"
#include "ftcsettings.h"
#include "private/queryresult.h"
#include "private/httpclient.h"
#include <QFileInfo>

FtcResultFlag FtcUploadFileInfo::flag() const
{
	return m_flag;
}
QString FtcUploadFileInfo::newFileId() const
{
	return m_newFileId;
}
QString FtcUploadFileInfo::oldFileId() const
{
	return m_oldFileId;
}
QString FtcUploadFileInfo::errMsg() const
{
	return m_errMsg;
}
void FtcUploadFileInfo::setFlag(const FtcResultFlag flagInfo)
{
	m_flag = flagInfo;
}
void FtcUploadFileInfo::setNewFileId(const QString& fileId)
{
	m_newFileId = fileId;
}
void FtcUploadFileInfo::setOldFileId(const QString& fileId)
{
	m_oldFileId = fileId;
}
void FtcUploadFileInfo::setErrMsg(const QString& msg)
{
	m_errMsg = msg;
}
FtcResultFlag FtcDownloadFileInfo::flag() const
{
	return m_flag;
}
QString FtcDownloadFileInfo::errMsg() const
{
	return m_errMsg;
}
void FtcDownloadFileInfo::setFlag(const FtcResultFlag flagInfo)
{
	m_flag = flagInfo;
}
void FtcDownloadFileInfo::setErrMsg(const QString& errMsg)
{
	m_errMsg = errMsg;
}
FtcResultFlag FtcDownloadImageInfo::flag() const
{
	return m_flag;
}
QImage FtcDownloadImageInfo::image() const
{
	return m_image;
}
QString FtcDownloadImageInfo::errMsg() const
{
	return m_errMsg;
}
void FtcDownloadImageInfo::setFlag(const FtcResultFlag flagInfo)
{
	m_flag = flagInfo;
}
void FtcDownloadImageInfo::setImage(const QImage& imageObject)
{
	m_image = imageObject;
}
void FtcDownloadImageInfo::setErrMsg(const QString& errMsg)
{
	m_errMsg = errMsg;
}
void Ftc::setConnectTimeout(const long timeout)
{
	FtcSettings::setConnectTimeout(timeout);
}
void Ftc::setRequestTimeout(const long timeout)
{
	FtcSettings::setRequestTimeout(timeout);
}
void Ftc::setRootUrl(const QString& rootUrl)
{
	FtcSettings::setRootUrl(rootUrl);
}
FtcDownloadFileInfo Ftc::downloadFile(const QString& fileId, const QString& saveFullName)
{
	QString saveFileFullName = saveFullName;
	return downloadFile(fileId, saveFileFullName, false);
}
FtcDownloadFileInfo Ftc::downloadFileAutoExt(const QString& fileId, QString& saveFullName)
{
	return downloadFile(fileId, saveFullName, true);
}
FileInfo Ftc::queryFileInfo(const QString& fileId)
{
	std::cout << "查询文件信息：" << fileId.toStdString() << std::endl;
	const QString url = FtcSettings::pathFileInfo();
	std::string param;
	param = "{\"fileId\":\"";
	param.append(fileId.toStdString());
	param.append("\"}");
	std::cout << "请求地址: " << url.toStdString() << std::endl;
	std::cout << "请求参数: " << param << std::endl;
	cxi_ftc::HttpClient httpClient(url);
	if (std::string response; httpClient.post(param, response))
	{
		std::cout << "请求结果: " << response << std::endl;
		QueryResult queryResult;
		if (const bool rst = serialization::deserializeJson(response, queryResult); rst && queryResult.success)
		{
			return queryResult.data;
		}
	}
	else
	{
		std::cout << "请求失败: " << httpClient.httpCode() << "," << httpClient.error().toStdString() << std::endl;
	}
	FileInfo fileInfo;
	return fileInfo;
}
FtcDownloadFileInfo Ftc::downloadFile(const QString& fileId, QString& saveFullName, const bool autoExt)
{
	FtcDownloadFileInfo resultInfo;
	if (const FileInfo fileInfo = queryFileInfo(fileId);fileInfo.isEmpty())
	{
		resultInfo.setFlag(FtcResultFlag::Fail);
		resultInfo.setErrMsg("查询文件信息失败。");
	}
	else
	{
		//自动补全扩展名
		if (const QFileInfo saveFileInfo(saveFullName);
			autoExt && saveFileInfo.suffix().isEmpty() && !fileInfo.fileType.isEmpty())
		{
			saveFullName.append("." + fileInfo.fileType);
		}

		if (cxi_ftc::HttpClient httpClient(fileInfo.fileFullPath);httpClient.downloadFile(saveFullName))
		{
			resultInfo.setFlag(FtcResultFlag::Success);
			resultInfo.setErrMsg(""+httpClient.httpCode());
		}
		else
		{
			resultInfo.setFlag(FtcResultFlag::Fail);
			resultInfo.setErrMsg(httpClient.error());
		}
	}

	return resultInfo;
}
